package com.saxon.blog.controller;


import com.github.pagehelper.PageInfo;
import com.saxon.blog.pojo.Blog;
import com.saxon.blog.pojo.BlogType;
import com.saxon.blog.service.Blog.BlogService;
import com.saxon.blog.service.Blog.BlogTypeService;
import com.saxon.blog.service.Blog.BlogTypeServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BlogController {
    private BlogService blogService;

    private BlogTypeService blogTypeService;

    @Autowired
    public void setBlogService (BlogService blogService) {
        this.blogService = blogService;
    }

    @Autowired
    public void setBlogTypeService (BlogTypeService blogTypeService) {
        this.blogTypeService = blogTypeService;
    }

    @GetMapping ("/blog/getBlog")
    @ResponseBody
    public List<Blog> getBlog () {
        Map<String, Object> map = new HashMap<> ();
        map.put ("status", 1);
        List<Blog> blogs = blogService.QueryBlog (map);
        return blogs;
    }

    @ApiOperation ("获得最新的5篇文章")
    @GetMapping ("/blog/getBlogNew")
    @ResponseBody
    public List<Blog> getBlogNew () {
        return blogService.QueryBlogNew ();
    }

    @ApiOperation ("根据页数查询博客")
    @PostMapping ("/blog/getBlogBypage/")
    @ResponseBody
    public List<Blog> getAllBlogByPage (int pageNum, Integer Tid) {
        int pageSize = 5;
        Map<String, Object> map = new HashMap<> ();
        if (Tid != null) {
            map.put ("type_id", Tid);
        }
        map.put ("status", 1);
        PageInfo<Blog> allTypeByPage = blogService.getAllBlogByPage (pageNum, pageSize, map);
        return allTypeByPage.getList ();
    }

    @ApiOperation ("查询博客的总页数")
    @PostMapping ("/blog/getBlogNum")
    @ResponseBody
    public int getBlogNum (Integer Tid) {
        Map<String, Object> map = new HashMap<> ();
        if (Tid != null) {
            map.put ("type_id", Tid);
        }
        int totalCount = blogService.QueryBlogNum (map);
        int pageCount = totalCount / 5;
        if (pageCount * 5 <= totalCount) {
            pageCount++;
        }
        return pageCount;
    }

    @GetMapping ("details/getBlogById")
    @ResponseBody
    public Blog getBlogById (int pid) {
        System.out.println (pid);
        Map<String, Object> map = new HashMap<> ();
        map.put ("id", pid);
        List<Blog> blogs = blogService.QueryBlog (map);
        return blogs.get (0);
    }

    @GetMapping ("details/{id}")
    @ResponseBody
    public Blog getBlogUseRestful (@PathVariable int id) {
        Map<String, Object> map = new HashMap<> ();
        map.put ("id", id);
        List<Blog> blogs = blogService.QueryBlog (map);
        return blogs.get (0);
    }

    //-----------------------后台


    //    管理后台代码
    @ApiOperation ("获得博客的种类")
    @GetMapping ("/admin/getBlogWithType")
    @ResponseBody
    public List<Blog> queryBlogWithType () {
        return blogService.queryBlogWithType (null);
    }

    @ApiOperation ("获得博客的总数")
    @GetMapping ("/admin/blogNum")
    @ResponseBody
    public int queryBlogNum () {
        return blogService.QueryBlogNum (null);
    }

    @ApiOperation ("得到博客列表，有页数")
    @GetMapping ("admin/page/BlogList/{pageNum}")
    public String QueryBlog (@PathVariable int pageNum, Model model) {
        Map<String, Object> map = new HashMap<> ();
        int totalCount = blogService.QueryBlogNum (map);
        int pageCount = totalCount / 10;
        if (pageCount * 5 <= totalCount) {
            pageCount++;
        }
        if (pageNum > pageCount) {
            pageNum = pageCount;
        }
        if (pageNum < 1) {
            pageNum = 1;
        }
        PageInfo<Blog> blogs = blogService.getAllBlogByPage (pageNum, 10, null);
        List<Blog> list = blogs.getList ();
        //获得博客的页数
        model.addAttribute ("pageCount", pageCount);
        model.addAttribute ("pageNum", pageNum);
        model.addAttribute ("blogs", list);
        return "admin/page/BlogList";
    }

    @ApiOperation ("通过ID删除博客")
    @DeleteMapping ("/admin/deleteBlog")
    @ResponseBody
    public int deleteBlogById (int id) {
        return blogService.deleteBlogById (id);
    }

    @ApiOperation ("通过选中的id删除博客")
    @DeleteMapping ("/admin/deleteBlogByIds")
    @ResponseBody
    public int deleteUserByIds (String[] checked) {
        int a = 0;
        if (checked != null) {
            for (int i = 1; i < checked.length - 1; i++) {
                int id = Integer.parseInt (checked[i].split ("\"")[1]);
                int i1 = blogService.deleteBlogById (id);
                if (i1 > 0) {
                    a++;
                }
            }
        }
        return a;
    }

    @ApiOperation ("添加一个博客")
    @GetMapping ("/admin/addBlog")
    @ResponseBody
    public boolean addBlog (
            String title, String introduction, String keyword, String content, String status, int type_id, String image) {
        Date date = new Date ();
        Map<String, Object> map = new HashMap<> ();
        map.put ("title", title);
        map.put ("introduction", introduction);
        map.put ("keyword", keyword);
        map.put ("content", content);
        map.put ("CreateTime", date);
        map.put ("ModifyTime", date);
        map.put ("status", status);
        map.put ("type_id", type_id);
        map.put ("image", image);
        int i = blogService.insertBlog (map);
        return i > 0;
    }

    @ApiOperation ("通过ID获得博客")
    @GetMapping ("/admin/getBlog/{id}")
    public String getBlog (@PathVariable int id, Model model) {
        Map<String, Object> map = new HashMap<> ();
        map.put ("id", id);
        List<Blog> blogs = blogService.QueryBlog (map);
        List<BlogType> blogTypes = blogTypeService.QueryAllType ();
        model.addAttribute ("blogTypes", blogTypes);
        model.addAttribute ("blog", blogs.get (0));
        return "/admin/page/editBlog";
    }

    /*
    //id, title, introduction, keyword, content, CreateTime, ModifyTime, status, type_id
    @ApiOperation ("查找博客")
    @GetMapping ("/admin/searchBlog/")
    @ResponseBody
    public int searchBlog (String key, String value,Model model) {
        Map<String, Object> map = new HashMap<> ();
        switch (key) {
            case "id":
                map.put ("id", value);
                break;
            case "title":
                map.put ("title", value);
                break;
            case "keyword":
                map.put ("keyword", value);
                break;
            case "status":
                if (value.equals ("禁用")) {
                    map.put ("status", 0);
                } else {
                    map.put ("status", 1);
                }
                break;
            case "type_id":
                BlogType blogType = blogTypeService.QueryBlogTypeByName (value);
                if (blogType != null) {
                    map.put ("type_id", blogType.getId ());
                }
                break;
        }
        PageInfo<Blog> blog = blogService.getAllBlogByPage (1, 10, map);
        List<Blog> list = blog.getList ();
        model.addAttribute ("blogs",list.get (0));
        return list.size ();
    }
*/
    @ApiOperation ("修改博客的状态")
    @GetMapping ("/admin/alertBlogAnthen")
    @ResponseBody
    public int alertBlogAnthen (String[] checked, int status) {
        Map<String, Object> map = new HashMap<> ();
        map.put ("status", status);
        int flag = 0;
        if (checked != null) {
            for (int i = 1; i < checked.length - 1; i++) {
                int id = Integer.parseInt (checked[i].split ("\"")[1]);
                map.put ("id", id);
                int i1 = blogService.updateBlog (map);
                if (i1 != 0) {
                    flag++;
                }

            }
        }
        return flag;
    }

    //id, title, introduction, keyword, content, CreateTime, ModifyTime, status, type_id
    @ApiOperation ("修改一个博客")
    @GetMapping ("/admin/page/updateBlog")
    @ResponseBody
    public boolean updateBlog (int id,
                           String title, String introduction, String keyword, String content, String status, int type_id, String image) {
        Date date = new Date ();
        Map<String, Object> map = new HashMap<> ();
        map.put ("id", id);
        map.put ("title", title);
        map.put ("introduction", introduction);
        map.put ("keyword", keyword);
        map.put ("content", content);
        map.put ("ModifyTime", date);
        map.put ("status", status);
        map.put ("type_id", type_id);
        map.put ("image", image);
        return blogService.updateBlog (map)>0;
    }

}
